package org.techmore.biz;

import org.techmore.utils.DatabaseHelper;
import org.techmore.utils.PropertiesUtil;
import org.techmore.vo.CouponDetailsVO;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class MyCouponDetailsActivityDataHandler {
	
	private static MyCouponDetailsActivityDataHandler myCouponDetailsActivityDataHandler = new MyCouponDetailsActivityDataHandler();
	
	public static MyCouponDetailsActivityDataHandler getInstance(){
		return myCouponDetailsActivityDataHandler;
	}
	
	private String databaseName = null;
	private SQLiteDatabase sqLiteDatabase = null;
	private Cursor cursor = null;
	
	public boolean deleteCoupon(Context context,String couponId,String couponNo){
		boolean deleteBool = false;
		databaseName = PropertiesUtil.getInstance().getValue("databaseName");
		String sql = "delete from couponDetailsVO where couponSeriesNo=? and couponNo=?";
		
		try {
			sqLiteDatabase = new DatabaseHelper(context, databaseName).getWritableDatabase();
			sqLiteDatabase.execSQL(sql, new Object[]{couponId,couponNo});
			deleteBool = true;
		} catch (Exception e) {
			Log.e("sqlite:", e.getMessage());
		}finally{
			if(null != sqLiteDatabase){
				sqLiteDatabase.close();
				sqLiteDatabase = null;
			}
		}
		return deleteBool;
	}
	
	
	public CouponDetailsVO getCouponDetailsVO(Context context,String couponId,String couponNo){
		CouponDetailsVO vo = new CouponDetailsVO();
		databaseName = PropertiesUtil.getInstance().getValue("databaseName");
		String sql ="select * from couponDetailsVO where couponSeriesNo=? and couponNo=?";
		try {
			sqLiteDatabase = new DatabaseHelper(context, databaseName).getReadableDatabase();
			cursor = sqLiteDatabase.rawQuery(sql, new String[]{couponId.trim(),couponNo.trim()});
			while(cursor.moveToNext()){
				/*
				 * couponSeriesNo varchar(24),store varchar(100),subject varchar(100),img48xUrl varchar(200),
				 * couponImgUrl varchar(200),
				 * startDate varchar(20),endDate varchar(20),
				 * introduction text,couponNo varchar(30),qrImageUrl varchar(200))
				 */
				vo.setCouponSeriesNo(cursor.getString(cursor.getColumnIndex("couponSeriesNo")));
				vo.setSubject(cursor.getString(cursor.getColumnIndex("subject")));
				vo.setStore(cursor.getString(cursor.getColumnIndex("store")));
				vo.setCouponNo(cursor.getString(cursor.getColumnIndex("couponNo")));
				vo.setStartDate(cursor.getString(cursor.getColumnIndex("startDate")));
				vo.setEndDate(cursor.getString(cursor.getColumnIndex("endDate")));
				vo.setIntroduction(cursor.getString(cursor.getColumnIndex("introduction")));
				
			}
		} catch (Exception e) {
			Log.e("sqlite:", e.getMessage());
		}finally{
			if(null != cursor){
				cursor.close();
				cursor = null;
			}
			if(null != sqLiteDatabase){
				sqLiteDatabase.close();
				sqLiteDatabase = null;
			}
		}
		return vo;
	}


}
